<?php
namespace App\Http\Controllers\Api\V2\Auth;

use App\Http\Controllers\ApiController;
use App\Http\Controllers\Business\App\Auth\AppAuth;
use App\Http\Controllers\Business\App\MyCenter\MyCenter;
use Illuminate\Support\Facades\Auth;
use Symfony\Component\HttpFoundation\Request;
use Log;

class AuthController extends ApiController
{
    //获取手机验证码
    public function getCode(){
        $mobile = $this->request->get('mobile');
        if($this->request->has('type')) {
            $type = $this->request->get('type');
        } else {
            $type = '1';
        }
        if($this->request->has('mode')) {
            $mode = $this->request->get('mode');
        } else {
            $mode = '1';
        }
        $data = AppAuth::getCode($mobile,$type, $mode);
        if($data['status'] == 'success') {
            return $this->responseSuccessMessage($data['message']);
        } else {
            return $this->responseError($data['message']);
        }
    }
    //注册或登录
    //type的值 为0表示普通登录；为1表示微信登录；为2表示qq登录，为3表示微博登录
    public function registerOrLogin(){
        $mobile = $this->request->get('mobile');
        $mobile = trim($mobile);
        $code = $this->request->get('code');
        $code = trim($code);
        $avatar = $this->request->get('avatar');
        $nickname = $this->request->get('nickname');
        if($this->request->has('openid')) {
            $openId = $this->request->get('openid');
        } else {
            $openId = '';
        }
        if($this->request->has('type')) {
            $type = $this->request->get('type');
        } else {
            $type = '0';
        }
        $channel = $this->request->get('channel');
        $deviceId = $this->request->get('deviceid');
        $osVersion = $this->request->get('osversion');
        $phoneType = $this->request->get('phonetype');
        $versionCode = $this->request->get('versioncode');
        $qrCodeUrl = $this->request->get('qrcodeurl');
        $ip = $this->request->ip();
        $localCity = $this->request->get('localcity');
        if($versionCode >= 15) {
            $data = AppAuth::registerOrLoginByV15Version($mobile, $code,$qrCodeUrl,$avatar,$nickname,$openId,$type,$channel,$deviceId,$osVersion,$phoneType,$versionCode, $ip, $localCity);
        } else {
            $data = AppAuth::registerOrLogin($mobile,$code,$avatar,$nickname,$openId,$type,$channel,$deviceId,$osVersion,$phoneType,$versionCode,$ip,$localCity);
        }
        if($data['status']=='success') {
        $userId = $data['data']['userid'];
        $myCenterResult = MyCenter::getUserInfoByUserId($userId);
        if($myCenterResult['status'] == 'success') {
            $returnData = $myCenterResult['data'];
        }
        $tokenResult = AppAuth::getToken($userId);
        if($tokenResult['status'] == 'success') {
            $returnData['token'] = $tokenResult['data'];
        } else {
            $returnData['token'] = '';
        }
        return $this->responseSuccessData($returnData);
        } else {
            return $this->responseError($data['message']);
        }
    }
    //验证验证码
    public function verifyCode() {
        $mobile = $this->request->get('mobile');
        $code = $this->request->get('code');
        $result = AppAuth::validataSmsCode($mobile, $code);
        if($result['status'] == 'failed') {
            return $this->responseError($result['message']);
        } else {
            return $this->responseSuccessMessage($result['message']);
        }
    }
    //第三方登录 type的值为1表示微信登录；为2表示qq登录，为3表示微博登录
    public function thirdLogin() {
        $result = AppAuth::thirdLogin($this->request->get('openid'), $this->request->get('type'));
        if($result['status'] == 'failed') {
            return $this->responseError($result['message']);
        }
        $data = $result['data'];
        $userId = $data['userid'];
        $isRegistered = $data['isregistered'];
        $tokenResult = AppAuth::getToken($userId);
        $myCenterResult = MyCenter::getUserInfoByUserId($userId);
        if($myCenterResult['status'] == 'success') {
            $returnData = $myCenterResult['data'];
        }
        if($tokenResult['status'] == 'success') {
            $returnData['token'] = $tokenResult['data'];
        } else {
            $returnData['token'] = '';
        }
        $returnData['isregistered'] = $isRegistered;
        return $this->responseSuccessData($returnData);
    }
    //判断是否第一次登录
    public function isFirstLogin(Request $request) {
        $result = AppAuth::isFirstLogin($request->get('mobile'));
        if($result['status'] == 'failed') {
            return $this->responseError($result['message']);
        }
        $message = '该用户是首次登录';
        $data = $result['data'];
        return $this->responseSuccess($message, $data);
    }
    //二个小时内绑定地推二维码
    public function batchSalesmanQrCodeAfterTwoHourRegister() {
        $userId = $this->userId;
        $qrCodeUrl = $this->request->get('qrcodeurl');
        $deviceId = $this->request->get('deviceid');
        $phoneType = $this->request->get('phonetype');
        $channel = $this->request->get('channel');
        $localCity = $this->request->get('localcity');
        $ip = $this->request->ip();
        $result = AppAuth::batchSalesmanQrCodeAfterTwoHourRegister($userId, $qrCodeUrl, $deviceId, $phoneType, $channel, $ip, $localCity);
        if($result['status'] == 'failed') {
            return $this->responseError('你已经是老用户了！');
        } else {
            return $this->responseSuccessMessage($result['message']);
        }
    }
    //刷新token
    public function refreshToken() {
        $token=AppAuth::refreshToken($this->request->get('token'));
        if($token) {
            $data = ['token'=>$token];
            return $this->responseSuccessData($data);
        } else {
            $message = '认证失败，重新登录';
            return $this->responseError($message);
        }
    }
    //登出
    public function logout() {
        AppAuth::apiLogout($this->request->get('token'));
        $message = '退出成功！';
        $data = [];
        return $this->responseSuccess($message, $data);
    }
    
}